package com.springboot.template.thread.runnable;

public class RunnableDemo implements Runnable{

    private Thread t;
    private String threadName;

    public RunnableDemo(String threadName) {
        this.threadName = threadName;
        System.out.println("Creating threadName = " + threadName);
    }

    @Override
    public void run() {
        System.out.println("Running threadName = " + threadName);
        for (int i = 4; i > 0; i--) {
            System.out.println("Thread threadName =" + threadName + "," + i);
            //现成休眠
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                System.out.println("Thread " + threadName + "interrupted");
            }
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting threadName = " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

    public static void main(String[] args) {
        RunnableDemo r1 = new RunnableDemo("Thread-1");
        r1.start();
        RunnableDemo r2 = new RunnableDemo("Thread-2");
        r2.start();
    }
}
